def pr(num):
    for i in range(2, int(num)):
        if num % i == 0:
            return False
    return True


def max_pr(num):
    if num < 1000 or num > 9999:
        return 0

    t4 = num // 1000
    t3 = (num % 1000) // 100
    t2 = (num % 100) // 10
    t1 = num % 10
    max_ch = 0
    for first in (t1, t2, t3, t4):
        for second in (t4 * 10 + t3, t3 * 10 + t2, t2 * 10 + t1):
            for third in (t4 * 100 + t3 * 10 + t2, t3 * 100 + t2 * 10 + t1):
                for tek_ch in (first, second, third, num):
                    if pr(tek_ch) and tek_ch > max_ch:
                        max_ch = tek_ch
    return max_ch


num = int(input())
result = max_pr(num)
print(result)